const { app, BrowserWindow, ipcMain } = require('electron/main');
const path = require('node:path');
const fd = require('./fueldo');
const test = require('./test');
var timerId;
const createWindow = () => {
  const win = new BrowserWindow({
    width: 400,
    height: 350,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  win.loadFile('index.html');

};
ipcMain.on('exportBtn', (event, psw) => {
  (async () => {
    const reesult = await fd.exportAddresses(psw);
    console.log(reesult);
    event.reply('exportResult', reesult);

  })();
});

ipcMain.on('start', (event, psw, min, startItem, endItem) => {
  startItem = parseInt(startItem);
  endItem = parseInt(endItem);
  clearTimeout(timerId);
  refreshAccount(event, psw, min, startItem, endItem);
});

function refreshAccount(event, psw, min, startItem, endItem) {
  (async () => {
    let currentAdd = await fd.refreshAccount(psw, startItem);
    fd.startGame(psw);

    let sec = min * 60;
    // 设置一个定时器，然后取消它
    timerId = setInterval(function () {
      sec = sec - 1;
      event.reply('refreshAccount', startItem, currentAdd, sec);
      if (sec <= 0) {
        clearTimeout(timerId);
        let startItem1 = startItem + 1;
        if (startItem1 < endItem || !endItem) {
          refreshAccount(event, psw, min, startItem1, endItem);
        }
      }
      console.log(sec);

    }, 1000);

  })();
}

app.whenReady().then(() => {
  createWindow();
  app.on('activate', async () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();

    }
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});


